package com.songcha.library_common.handler

import android.view.KeyEvent
import androidx.appcompat.app.AppCompatActivity
import com.songcha.library_common.util.ToastUtil
import java.util.Timer
import java.util.TimerTask
import kotlin.system.exitProcess

class AppExitTipHandler {

    companion object{
        private var isExit = false
        private var tExit: Timer? = null

        @JvmStatic
        fun handleOnKeyUp(activity: AppCompatActivity,keyCode: Int, block:()->Boolean):Boolean{
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                if (isExit === false) {
                    isExit = true
                    if (tExit != null) {
                        tExit!!.cancel()
                    }
                    tExit = Timer()
                    val task: TimerTask = object : TimerTask() {
                        override fun run() {
                            isExit = false
                        }
                    }
                    ToastUtil.show("再按一次退出程序")
                    //delay 2 second to trigger
                    tExit?.schedule(task, 2000)
                } else {
                    activity.finish()
                    exitProcess(0)
                }
                return true
            }
            return block()
        }
    }
}